/*
 * @(#)RepositoryClients.java
 * Date 2013-10-21
 * Version 1.0
 * Author Jim Horner
 * Copyright (c)2012
 */


package us.hornerscorners.lamppost.xca.model;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import javax.enterprise.context.ApplicationScoped;

import javax.inject.Inject;

import ihe.iti.xds_b._2007.DocumentRepositoryService;

import us.hornerscorners.lamppost.config.EndpointConfig;

import us.hornerscorners.lamppost.ihecommon.config.IHEConfig;
import us.hornerscorners.lamppost.ihecommon.config.IHEServiceType;
import us.hornerscorners.lamppost.ihecommon.config.XCAGatewayConfig;

import us.hornerscorners.lamppost.repository.client.RepositoryClient;

import us.hornerscorners.lamppost.repository.service.AtnaRepositoryService;


/**
 * Class description
 *
 *
 * @version        v1.0, 2013-10-21
 * @author         Jim Horner
 */
@ApplicationScoped
public class RepositoryClients implements IRepositoryClients {

    /** Field description */
    @Inject
    private AtnaRepositoryService atnaService;

    /** Field description */
    private final Map<String, RepositoryClient> clients;

    /** Field description */
    @Inject
    private IHEConfig config;

    /** Field description */
    private final DocumentRepositoryService repositoryService;

    /**
     * Constructs ...
     *
     *
     */
    public RepositoryClients() {

        super();

        this.repositoryService = new DocumentRepositoryService();
        this.clients = new HashMap<>();
    }

    /**
     * Method description
     *
     *
     * @param repositoryId
     *
     * @return
     */
    @Override
    public boolean contains(String repositoryId) {

        return this.clients.containsKey(repositoryId);
    }

    /**
     * Method description
     *
     *
     * @param repositoryId
     *
     * @return
     */
    @Override
    public RepositoryClient find(String repositoryId) {

        return this.clients.get(repositoryId);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public RepositoryClient[] findAll() {

        Collection<RepositoryClient> result = this.clients.values();

        return result.toArray(new RepositoryClient[result.size()]);
    }

    /**
     * Method description
     *
     */
    @PostConstruct
    public void postConstruct() {

        XCAGatewayConfig serviceConfig =
            (XCAGatewayConfig) this.config.findServiceConfig(IHEServiceType.XCA);

        for (String id : serviceConfig.getRepositories()) {

            EndpointConfig endpoint = this.config.findServiceEndpoint(id);

            if (endpoint == null) {

                String msg =
                    String.format("Unknown repository in XCA Service Config: [%s].", id);

                throw new IllegalStateException(msg);
            }

            RepositoryClient client = new RepositoryClient(this.atnaService,
                                          this.repositoryService, endpoint);

            this.clients.put(id, client);
        }
    }
}
